// mobile bind
// ============

define(['zepto', 'sui', 'utils', 'JST', 'wx'], function($, sui, utils, JST, wx){
  return {
    init: function(data) {
      this.data = data;
      this.latitude = null;
      this.longitude = null;
      // forbidden wx share
      this.initWx(data.jsConfig, false);
    },
    
    initWx: function(config, shareable) {
      var isDebug = location.search.indexOf('debug=true') > 0
      var apis = (shareable == false ? ['hideMenuItems', 'getLocation'] : ['onMenuShareTimeline', 'onMenuShareAppMessage', 'getLocation'])
      var that = this;
      wx.config({
        debug: isDebug,
        appId: config.appid,
        timestamp: config.timestamp,
        nonceStr: config.nonceStr,
        signature: config.signature,
        jsApiList: apis
      })
      
      wx.ready(function() {
        if(shareable == false) {
          wx.hideOptionMenu();
        } else {
          wx.onMenuShareTimeline(that.getShareMessage(shareData));
          wx.onMenuShareAppMessage(that.getShareMessage(shareData));
        }
        
        wx.getLocation({
          type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
          success: function (res) {
            that.latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
            that.longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
            that.speed = res.speed; // 速度，以米/每秒计
            that.accuracy = res.accuracy; // 位置精度
          },
          cancel: function (res) {
            $("img.qrcode").addClass("hide");
            $("p.loading-tips").html("<span class='color-danger'>请允许系统获取地理位置信息，以便生成二维码。请刷新页面，允许获取后重试！</span>")
          }
        });
        that.rebuildQrcode();
      })
    },
    
    rebuildQrcode: function() {
      var that = this;
      $("#qrcode-create a").on("click", function(e){
        e.preventDefault();
        $(this).addClass("hide");
        $("img.qrcode").removeClass("hide");
        $(".loading-tips").empty().html("二维码加载中....");
        that.getQrcode();
      })
    },
    
    getQrcode: function() {
      var that = this;
      var isDebug = location.search.indexOf('debug=true') > 0
      $.ajax({
          type: "POST",
          url:  window.__CTX__.root + "/profile/course-schedules/" + that.data.scheduleId + "/qrcode",
          timeout: 7000,
          data: {
            "longitude": that.longitude,
            "latitude": that.latitude,
            "debug": isDebug
          }
        }).done(function(data) {
          data = JSON.parse(data)
          if(parseInt(data.code) == 0) {  
            $("img.qrcode").attr("src", window.__CTX__.cdn + "/" + data.data.qrcode)
                           .css("width", "100%");
            $("p.loading-tips").empty().html("有效期60s, 失效后刷新页面可重新获取。")
          } else {
            $.toast(data.message);
            $("img.qrcode").addClass("hide");
            $("p.loading-tips").empty().html("生成失败，在符合下列条件情况下，请刷新页面重新生成。<span class='color-danger'><br>1：在门店1000米范围内才能生成签到二维码。<br>2：生成的二维码签到有效期为60s,失效后请刷新。</span>")
          }
        }).fail(function() {
          $("img.qrcode").addClass("hide");
          $.toast("生成二维码失败，点击按钮刷新重试");
          $("p.loading-tips").empty().html("生成失败，点击上方按钮进行刷新。");
        })
      
    }
  }
})

